
BossAI = class("BossAI")



function BossAI:ctor(gameObject)
    self.BossAIState = {
        Enter = 0,
        Idle = 1,
        Move = 2,
        Attack = 3,
    }
    self.State = self.BossAIState.Enter

    --self对应的对象的实例本身的表
    self.gameObject = gameObject
    self.transform = self.gameObject.transform

    self.fire_time = 0
    self.last_time = 0
    self.Speed = 2
    self.direction = Vector3.zero
    self.bulletSpeed = 5
    self.bossHP = 20

    local luaTrigger = self.gameObject:AddComponent(typeof(LuaRigidTrigger))
    luaTrigger:AddHandler(self.OnTriggerEnter2D,self)

    UpdateBeat:Add(self.Update,self)
end

function BossAI:Update()
    if self.State == self.BossAIState.Enter then
        self:OnEnter()
    elseif self.State == self.BossAIState.Idle then
        self:OnIdle()
    elseif self.State == self.BossAIState.Move then
        self:OnMove()
    elseif self.State == self.BossAIState.Attack then
        self:OnAttack()
    end   
end

function BossAI:OnEnter()
    self.transform:Translate(Vector3.down * self.Speed * Time.deltaTime)
    if (self.transform.position.y<=2.5) then
        self.last_time = Time.time 
        self.State = self.BossAIState.Idle
    end
end

function BossAI:OnIdle()

    self:CheckFire()
    if (Time.time - self.last_time > 1) then
        self.last_time = Time.time
        self.State = self.BossAIState.Move      
    
        --计算移动方向
        if (self.transform.position.x < 0) then
            self.direction = Vector3.right
        else
            self.direction = Vector3.left
        end
    end
end

function BossAI:OnMove()
    self.transform:Translate(self.direction * self.Speed * Time.deltaTime)

    if (self.transform.position.x>2 or self.transform.position.x<-2) then
        self.last_time = Time.time
        self.State = self.BossAIState.Attack
    end
end

function BossAI:OnAttack()
    self:CheckFire()
    if (Time.time - self.last_time > 2) then
        self.last_time = Time.time
        self.State = self.BossAIState.Idle
    end
end

function BossAI:CheckFire()
    
    if ( Time.time - self.fire_time > 0.3 ) then
        self.fire_time = Time.time
        self.num = math.random(1,2)
        if (self.num ==1) then
            for i=1,18 do
                local bullet = newObject(ThunderManager.prefabs[3]).transform
                self.firepos = self.gameObject.transform:Find(i)
                bullet.transform.position = self.firepos.position
                bullet.transform.rotation = self.firepos.rotation
                bullet.gameObject:GetComponent(typeof(Rigidbody2D)).velocity = -self.firepos.up * self.bulletSpeed
                --销毁 敌机子弹（1秒）
                GameObject.Destroy(bullet.gameObject,1.5)
            end
        elseif (self.num==2)  then
            for i=1,54 do
                local bullet1 = newObject(ThunderManager.prefabs[10]).transform
                self.firepos =self.gameObject.transform:Find("pos"..i)
                bullet1.transform.position = self.firepos.position
                bullet1.transform.rotation = self.firepos.rotation
                bullet1.gameObject:GetComponent(typeof(Rigidbody2D)).velocity = -self.firepos.up * self.bulletSpeed
                --销毁 敌机子弹（1秒）
                GameObject.Destroy(bullet1.gameObject,1.5)
            end
        end
        
    end
end

function BossAI:OnTriggerEnter2D(collider)
    if (collider.gameObject:CompareTag("bullet")) then
        log("子弹打中Boss")

        --销毁 自己的子弹
        destroy(collider.gameObject)   

        self:hp()
      
    end
end

function BossAI:hp()
    
    log(self.bossHP.."======================")   
    self.bossHP =  self.bossHP - 1
    if ( self.bossHP<=0) then
        UpdateBeat:Remove(self.Update,self)
        --销毁 敌机
        destroy(self.gameObject)
         --加分
        Player.Score()
    end
end